$(function () {
    var init = true; //第一次加载树之后点击根节点


    $("#ejzh_new").click(function () {
        ejzh_new();
    });
    $("#ejzh_mod").click(function () {
        ejzh_modify();
    });
    $("#ejzh_del").click(function () {
        ejzh_delete();
    });
    $("#btn_ejzh_search").click(function () {
        ejzh_search();
    });

    $('#ejzh_grid').datagrid({
        url: $.webPath() + "/organ/ejzh/ejzhList",
        toolbar: "#toolbar_ejzh",
        fitColumns: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'nbgs_qc', title: '运营商', align: 'left', width: 50},
                {field: 'khh_jc', title: '开户行', align: 'left', width: 50},
                {field: 'kh_qc', title: '客户全称', align: 'left', width: 40},
                {field: 'qc', title: '帐户全称', align: 'left', width: 40},
                {field: 'jc', title: '帐户简称', align: 'left', width: 40},
                {field: 'zh', title: '帐号', align: 'right', width: 40},
                {field: 'ye', title: '余额', align: 'right', width: 30, formatter: function (value) {
                    var val = formatJe2(value, ",");
                    return val;
                }},
                {field: 'kyye', title: '可用余额', align: 'right', width: 30, formatter: function (value) {
                    var val = formatJe2(value, ",");
                    return val;
                }},
                {field: 'bzj', title: '保证金', align: 'right', width: 30, formatter: function (value) {
                    var val = formatJe2(value, ",");
                    return val;
                }}
              /*  ,
                {field: 'sxe', title: '授信额', align: 'right', width: 30, formatter: function (value) {
                    var val = formatJe2(value, ",");
                    return val;
                }}*/

            ]
        ],
        onLoadSuccess: function (target) {
            $("#ejzh_del").hide();
            $("#ejzh_mod").hide();
        },
        onClickRow: function (rowIndex, rowData) {
            $("#ejzh_del").show();
            $("#ejzh_mod").show();
        }
    });

});


var ejzhWin;
//新增公司机构
function ejzh_new() {
    ejzhWin = $.window({
        title: '添加二级账户',
        width: 750,
        height: 450,
        cache: false,
        url: $.webPath() + '/organ/ejzh/ejzh_content',
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: ejzh_saveItem
            }
        ]
    });
}
//修改二级账户
function ejzh_modify() {
    var ejzhId = $("#ejzh_grid").datagrid("getSelected").id;
    var ejzhYe = $("#ejzh_grid").datagrid("getSelected").ye;
    if (ejzhYe-0>0) {
        $.messager.alert("提示", "二级账户存在余额，不可修改!", "warning");
        return;
    }
    ejzhWin = $.window({
        title: '修改二级账户',
        width: 750,
        height: 450,
        cache: false,
        url: $.webPath() + '/organ/ejzh/ejzh_content?ejzhId=' + ejzhId,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: ejzh_updateItem
            }
        ]
    });
}
//删除公司机构
function ejzh_delete() {
    var ejzhId = $("#ejzh_grid").datagrid("getSelected").id;
    var ejzhYe = $("#ejzh_grid").datagrid("getSelected").ye;
    if (ejzhYe-0>0) {
        $.messager.alert("提示", "二级账户存在余额，不可删除!", "warning");
        return;
    }
    $.messager.confirm("确认", "确定删除此二级账户？", function (r) {
        if (r) {
            $.ajax({
                url: $.webPath() + "/organ/ejzh/delete_ejzh?ejzhId=" + ejzhId,
                type: "json",
                beforeSend: function () {
                    $("#ejzh_layout").mask();
                },
                success: function (data) {
                    $("#ejzh_layout").mask('hide');
                    data = $.parseJSON(data);
                    if (data.statusCode == '200') {
                        $('#ejzh_grid').datagrid('reload');
                        $.messager.show({
                            title: '提示框',
                            msg: data.message,
                            timeout: 3000,
                            showType: 'slide'
                        });
                    } else {
                        $.messager.alert('提示', data.message, 'info');
                    }
                }
            });
        }
    })
}
//保存
function ejzh_saveItem() {
    $(ejzhWin).mask();
    var url = $.webPath() + '/organ/ejzh/save_ejzh';
    $('#ejzhForm').form('submit', {
        url: url,
        onSubmit: function () {
            var valid = $(this).form('validate');
            if (!valid)
                $(ejzhWin).mask('hide');
            return valid;
        },
        success: function (data) {
            $(ejzhWin).mask('hide');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(ejzhWin).window("close");
                $('#ejzh_grid').datagrid('reload');
                $.messager.show({
                    title: '提示框',
                    msg: obj.message,
                    timeout: 3000,
                    showType: 'slide'
                });
            } else {
                $.messager.alert('提示', obj.message, 'error');
            }
        }
    });
}

function ejzh_updateItem() {
    $(ejzhWin).mask();
    var url = $.webPath() + '/organ/ejzh/update_ejzh';
    $('#ejzhForm').form('submit', {
        url: url,
        onSubmit: function () {
            var valid = $(this).form('validate');
            if (!valid)
                $(ejzhWin).mask('hide');
            return valid;
        },
        success: function (data) {
            $(ejzhWin).mask('hide');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(ejzhWin).window("close");
                $('#ejzh_grid').datagrid('reload');
                $.messager.show({
                    title: '提示框',
                    msg: obj.message,
                    timeout: 3000,
                    showType: 'slide'
                });
            } else {
                $.messager.alert('提示', obj.message, 'error');
            }
        }
    });
}

//公司机构树查询
function ejzh_search(id) {
    var qc = $('#ejzh_qc').val();
    var jc = $('#ejzh_jc').val();
    $('#ejzh_grid').datagrid('load',
        {
            qc: qc,
            jc: jc
        }
    );
}

